#coding=utf-8
#import libs
import _thread
import sys
import threading

import devices_python_cmd
import devices_python_sty
import Fun
import os
import tkinter
from   tkinter import *
import tkinter.ttk
import tkinter.font
from   PIL import Image,ImageTk

#Add your Varial Here: (Keep This Line of comments)
#Define UI Class
import devices_serial
from SysTrayIcon import SysTrayIcon


class  devices_python:
    def __init__(self,root,isTKroot = True):
        uiName = self.__class__.__name__
        Fun.Register(uiName,'UIClass',self)
        self.root = root
        Fun.Register(uiName,'root',root)
        style = devices_python_sty.SetupStyle()
        if isTKroot == True:
            root.title("检测设备连接服务")
            root.resizable(False,False)
            Fun.CenterDlg(uiName,root,820,908)
            root.iconbitmap(Fun.resource_path('logo.ico'))  # 更改窗口图标
            root['background'] = '#efefef'
        Form_1= tkinter.Canvas(root,width = 10,height = 4)
        Form_1.place(x = 0,y = 0,width = 820,height = 908)
        Form_1.configure(bg = "#efefef")
        Form_1.configure(highlightthickness = 0)
        Fun.Register(uiName,'Form_1',Form_1)
        #Create the elements of root 
        Text_9 = tkinter.Text(Form_1)
        Fun.Register(uiName,'Text_9',Text_9,'http_port')
        Text_9.place(x = 635,y = 18,width = 68,height = 18)
        Text_9.configure(relief = "sunken")

        Text_9.insert('end','8111')
        Text_9.config(state='disable')


        Label_10 = tkinter.Label(Form_1,text="http 服务端口:",width = 10,height = 4)
        Fun.Register(uiName,'Label_10',Label_10)
        Label_10.place(x = 535,y = 16,width = 100,height = 20)
        Label_10.configure(relief = "flat")
        PanedWindow_12 = tkinter.PanedWindow(Form_1)
        Fun.Register(uiName,'PanedWindow_12',PanedWindow_12)
        PanedWindow_12.place(x = 4,y = 49,width = 820,height = 548)
        PanedWindow_12.configure(orient = tkinter.HORIZONTAL)
        PanedWindow_12.configure(showhandle = "0")
        PanedWindow_12.configure(sashrelief = "flat")
        PanedWindow_12.configure(sashwidth = "3")
        PanedWindow_12_child1= tkinter.Canvas(PanedWindow_12,bg="#FFDD94",name="child1")
        PanedWindow_12_child1.place(x = 1,y = 1,width = 246,height = 546)
        PanedWindow_12.add(PanedWindow_12_child1,width =246)
        PanedWindow_12_child2= tkinter.Canvas(PanedWindow_12,bg="#D0E6A5",name="child2")
        PanedWindow_12_child2.place(x = 250,y = 1,width = 569,height = 546)
        PanedWindow_12.add(PanedWindow_12_child2,width =569)
        Fun.Register(uiName,'PanedWindow_12_child1',PanedWindow_12_child1)
        Fun.Register(uiName,'PanedWindow_12_child2',PanedWindow_12_child2)
        TreeView_15 = tkinter.ttk.Treeview(Form_1,show="tree")
        Fun.Register(uiName,'TreeView_15',TreeView_15,'devices_list')
        TreeView_15.place(x = 10,y = 54,width = 510,height = 530)
        TreeView_15.configure(show = "headings")
        TreeView_15.configure(selectmode = "extended")
        TreeView_15.configure(columns = ["编号","设备类型","设备名称","设备串口","波特率","备注"])
        TreeView_15.column("编号",anchor="center",width=40)
        TreeView_15.heading("编号",text="编号")
        TreeView_15.column("设备类型",anchor="center",width=89)
        TreeView_15.heading("设备类型",text="设备类型")
        TreeView_15.column("设备名称",anchor="center",width=89)
        TreeView_15.heading("设备名称",text="设备名称")
        TreeView_15.column("设备串口",anchor="center",width=89)
        TreeView_15.heading("设备串口",text="设备串口")
        TreeView_15.column("波特率",anchor="center",width=89)
        TreeView_15.heading("波特率",text="波特率")
        TreeView_15.column("备注",anchor="center",width=89)
        TreeView_15.heading("备注",text="备注")
        TreeView_15.bind("<Button-3>",Fun.EventFunction_Adaptor(devices_python_cmd.TreeView_15_onButton3,uiName=uiName,widgetName="TreeView_15"))
        TreeView_15.bind("<Double-Button-1>",Fun.EventFunction_Adaptor(devices_python_cmd.TreeView_15_onDoubleButton1,uiName=uiName,widgetName="TreeView_15"))
        Text_16 = tkinter.Text(Form_1)
        Fun.Register(uiName,'Text_16',Text_16,'device_status_text')
        Text_16.place(x = 531,y = 54,width = 280,height = 530)
        Text_16.configure(bg = "#000000")
        Text_16.configure(fg = "#ffffff")
        Text_16.configure(relief = "sunken")
        Text_16_Scrollbar = tkinter.Scrollbar(Text_16,orient=tkinter.VERTICAL)
        Text_16_Scrollbar.place(x = 260,y = 0,width = 20,height = 530)
        Text_16_Scrollbar.config(command = Text_16.yview)
        Text_16.config(yscrollcommand = Text_16_Scrollbar.set)
        Fun.Register(uiName,'Text_16_Scrollbar',Text_16_Scrollbar)
        Button_17 = tkinter.Button(Form_1,text="Button",width = 10,height = 4)
        Fun.Register(uiName,'Button_17',Button_17)
        Button_17.place(x = 763,y = 1,width = 48,height = 48)
        devices_python_cmd.ElementBGArray[17]=Image.open(Fun.resource_path("start.png")).convert('RGBA')
        devices_python_cmd.ElementBGArray_Resize[17] = devices_python_cmd.ElementBGArray[17].resize((48, 48),Image.ANTIALIAS)
        devices_python_cmd.ElementBGArray_IM[17] = ImageTk.PhotoImage(devices_python_cmd.ElementBGArray_Resize[17])
        Button_17.configure(image = devices_python_cmd.ElementBGArray_IM[17])
        Button_17.configure(command=lambda:devices_python_cmd.Button_17_onCommand(uiName,"Button_17"))
        Text_18 = tkinter.Text(Form_1)
        Fun.Register(uiName,'Text_18',Text_18,'http_status_text')
        Text_18.place(x = 7,y = 603,width = 807,height = 272)
        Text_18.configure(bg = "#000000")
        Text_18.configure(fg = "#ffffff")
        Text_18.configure(relief = "sunken")
        Text_18_Scrollbar = tkinter.Scrollbar(Text_18,orient=tkinter.VERTICAL)
        Text_18_Scrollbar.place(x = 787,y = 0,width = 20,height = 272)
        Text_18_Scrollbar.config(command = Text_18.yview)
        Text_18.config(yscrollcommand = Text_18_Scrollbar.set)
        Fun.Register(uiName,'Text_18_Scrollbar',Text_18_Scrollbar)
        #Create the Menu of root 
        MainMenu=tkinter.Menu(root)
        root.config(menu = MainMenu)
        设备=tkinter.Menu(MainMenu,tearoff = 0)
        设备.add_command(label="新建",command=lambda:devices_python_cmd.Menu_新建(uiName,"新建"))
        设备.add_command(label="修改",command=lambda:devices_python_cmd.Menu_修改(uiName,"修改"))
        设备.add_command(label="删除",command=lambda:devices_python_cmd.Menu_删除(uiName,"删除"))
        MainMenu.add_cascade(label="设备",menu=设备)

        帮助=tkinter.Menu(MainMenu,tearoff = 0)
        帮助.add_command(label="关于",command=lambda:devices_python_cmd.Menu_关于(uiName,"关于"))
        帮助.add_command(label="文档",command=lambda:devices_python_cmd.Menu_文档(uiName,"文档"))
        MainMenu.add_cascade(label="帮助",menu=帮助)
        #Inital all element's Data 
        Fun.InitElementData(uiName)
        #Call Form_1's OnLoad Function
        devices_python_cmd.Form_1_onLoad(uiName)
        #Add Some Logic Code Here: (Keep This Line of comments)

    def exitWindow(s, _sysTrayIcon=None):
        devices_serial.SYS_STATUS = False
        s.root.destroy()
    def exit(s, _sysTrayIcon=None):
        s.root.withdraw()
        #s.SysTrayIcon.activation()
        _thread.start_new_thread(thread_hidden,  (s.SysTrayIcon,))
    def hidden_window(s):
        s.root.withdraw()
        _thread.start_new_thread(thread_hidden,  (s.SysTrayIcon,))
        #s.SysTrayIcon.activation()
def thread_hidden(icon):
    icon.activation()

#Create the root of Kinter 
if  __name__ == '__main__':
    root = tkinter.Tk()
    MyDlg = devices_python(root)
    # 托盘图标右键菜单, 格式: ('name', None, callback),下面也是二级菜单的例子
    menu_options = ()
    hover_text = "检测设备串口服务"  # 悬浮于图标上方时的提示
    MyDlg.SysTrayIcon = SysTrayIcon(Fun.resource_path('logo.ico'), hover_text, menu_options, on_quit=MyDlg.exitWindow, tk_window=root, )

    root.bind("<Unmap>", lambda event: MyDlg.hidden_window() if root.state() == 'iconic' else False)
    root.protocol('WM_DELETE_WINDOW', MyDlg.exit)
    root.mainloop()
